package net.mengkang.nettyrest.utils; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.util.List; public class HttpRequestUtil { public static String post(String url, List<NameValuePair> params) { CloseableHttpClient httpClient = HttpClients.createDefault(); String responseText = ""; CloseableHttpResponse httpResponse = null; try { HttpPost httpPost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); httpPost.setConfig(requestConfig); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { responseText = EntityUtils.toString(httpResponse.getEntity()); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (httpResponse != null) { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } } return responseText; } public static String get(String url) { CloseableHttpClient httpClient = HttpClients.createDefault(); String responseText = ""; CloseableHttpResponse httpResponse = null; try { HttpGet httpGet = new HttpGet(url); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); httpGet.setConfig(requestConfig); httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { responseText = EntityUtils.toString(httpResponse.getEntity()); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (httpResponse != null) { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } } return responseText; } }